;
(function() {
  new Vue({
    el: '#projectDetail',
    data() {
      return {
        permissions: JSON.parse(window.localStorage.getItem('permissions')), //权限
        tabList: [{
            code: '',
            name: '项目详情'
          },
          {
            code: '',
            name: '跟进记录'
          },
          {
            code: '',
            name: '联系人'
          },
          {
            code: '',
            name: '相关文件'
          }
        ],
        currentTab: '', // 当前选中tab
        id: hlUrlUtil.GetQueryString('id'), //项目id
        from: hlUrlUtil.GetQueryString('from'),
        isShow_M: true,
        isShow_D: true,
        isClose: false,

        // 项目详情data
        channelCategoryList: [], //渠道分类
        channelNameList: [], //渠道名称
        processManagerList: [], //过程管理
        colse_reason_desc: '',
        close_reason_code: '',
        reasonList: [],
        data: {
          expansionProjInfo: {
            status: '',
            channelCategoryCode: '',
          },
          imagesUrl: [],
          customConfigProperty: '',
        },
        isShowPic: false,
        picUrl: '',
        // 跟进记录data
        showType: true,
        sortName: '#',
        sortFlag: 'ASC',
        followList: [],
        isScroll: false,
        lefthover: 0,
        tophover: 0,
        texthover: false,
        textLong: '',
        orders: 'asc',
        // 联系人data
        setType: '',
        add_edit_contact: false,
        contactList: [],
        contact_id: '',
        contact_name: '',
        contact_phone: '',
        contact_memo: '',
        contactItem: '',
        contactPerson: {

        },
        //相关文件data
        attachmentsList: [],
        progress: '',
        widthPic: '',
				tmpData: []
      }

    },
    filters: {
      formatDate: function(time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      max8lenOff(item) {
        if (!item) return '--'
        return item.length > 8 ? item.substr(0, 8) + '...' : item
      },
      formatUndefined: function(item) {
        if (!item) return '--'
        return item
      },

    },
    computed: {
      status: function() {
        switch (this.data.expansionProjInfo.status) {
          case 'YX':
            return '意向';
          case 'GB':
            return '关闭';
          case 'YY':
            return '运营'
        }
      },
    },
    methods: {
      //***  项目详情板块
      //展示大图
      showPic(url) {
        this.isShowPic = true
        this.picUrl = url
      },
      //详情编辑
      goToEdit() {
        hlUrlUtil.goTo('marketEdit', {
          'id': this.id,
          ddtab: true,
          'from': this.from
        }, true)
      },
      // 切换tab
      switchTab(val) {
        this.currentTab = val
      },
      // 关闭弹框
      closeReason() {
        this.isClose = true
        this.close_reason_code = ''
      },
      //选择关闭原因
      selectReason(code) {
        this.close_reason_code = code
      },
      // 获取项目详情
      getData() {
        var that = this
        hlhttp.get({
          url: 'market/expansion_proj_info',
          data: {
            expansion_proj_id: this.id
          },
          success: (data) => {
						this.tmpData = data.pageCustomizeComponentBizContentList ? data.pageCustomizeComponentBizContentList : []
            if (data.customConfigProperty) {
              data.customConfigProperty = JSON.parse(data.customConfigProperty)
            }
            this.data = data
            this.widthPic = 130 * data.imagesUrl.length

          }
        })
      },
      //获取关闭理由
      getReasonList() {
        var that = this
        hlhttp.get({
          url: 'configure/dict',
          data: {
            module_code: 'SCTZ',
            config_name: 'SCTZ_CLOSE_REASON'
          },
          success: function(data) {
            that.reasonList = data
          },
        })
      },
      // 确定关闭项目
      sureClose() {
        var that = this
        if (!this.close_reason_code) {
          hl.toast('请选择关闭原因')
          return
        }
        hlhttp.put({
          url: 'market/expansion_proj_info/close',
          data: {
            id: this.id,
            close_reason_code: this.close_reason_code,
            colse_reason_desc: this.colse_reason_desc || '#',
          },
          success: function(data) {
            hl.toast('关闭成功')
            that.isClose = false
            that.getData();
          },
        })
      },
      //跟进项目（去跟进项目页面）
      goTofollow() {
        hlUrlUtil.goTo('following', {
          'id': this.id,
          ddtab: true,
          'from': this.from
        }, true)
      },
      //***  跟进记录板块
      //显示hover
      hoverText(e, v) {
        var s = this.$refs.scrollLeft.scrollLeft;
        var l = e.srcElement.parentNode.offsetLeft - s;
        var t = e.srcElement.parentNode.offsetTop + 40
        this.lefthover = l + 'px';
        this.tophover = t + 'px';
        this.texthover = true;
        this.textLong = v;
      },
      mouseleaveOut() {
        this.lefthover = 0 + 'px';
        this.tophover = 0 + 'px';
        this.texthover = false;
        this.textLong = '';
      },
      sortByRecordTime() {
        if (this.orders == 'asc') {
          this.orders = 'desc'
        } else {
          this.orders = 'asc'
        }
        this.getfollowData()
      },
      showScroll() {
        this.isScroll = true;
      },
      hideScroll() {
        this.isScroll = false;
      },
      getfollowData() {
        hlhttp.get({
          url: 'market/Expansion_proj_followup_records',
          data: {
            ref_id: this.id,
            columns: 'CT',
            orders: this.orders,
          },
          success: (data) => {
            this.followList = data
          }
        })
      },
      //***  联系人
      // 获取联系人列表
      getContact() {
        hlhttp.get({
          url: 'market/expansion_proj_contacts',
          data: {
            ref_id: this.id
          },
          success: (data) => {
            data.projContactsList.map(item => {
              item.memo = item.memo || ''
            })
            this.contactList = data.projContactsList
          }
        })
      },
      add_editContact(type, item) {
        this.add_edit_contact = true
        this.setType = type
        if (type == 'add') {
          this.contact_name = ''
          this.contact_phone = ''
          this.contact_memo = ''
        } else {
          this.contact_id = item.id
          this.contact_name = JSON.parse(JSON.stringify(item.name))
          this.contact_phone = JSON.parse(JSON.stringify(item.phone))
          this.contact_memo = JSON.parse(JSON.stringify(item.memo))
        }
      },
      //添加或者编辑联系人
      setContact() {
        if (!this.contact_name) {
          hl.toast('请输入联系人')
          return
        }
        if (!this.contact_phone) {
          hl.toast('请输入联系方式')
          return
        }
        var that = this
        if (this.setType == 'add') {
          hlhttp.post({
            url: 'market/expansion_proj_contacts',
            data: {
              ref_id: this.id,
              name: this.contact_name,
              phone: this.contact_phone,
              memo: this.contact_memo || '#'
            },
            success: (data) => {
              that.contactList = data.projContactsList
              this.add_edit_contact = false
              that.getContact()
            }
          })
        } else {
          hlhttp.put({
            url: 'market/expansion_proj_contacts',
            data: {
              id: this.contact_id,
              name: this.contact_name,
              phone: this.contact_phone,
              memo: this.contact_memo || '#'
            },
            success: (data) => {
              that.contactList = data.projContactsList
              that.add_edit_contact = false
              that.getContact()
            }
          })
        }

      },
      //删除联系人
      delItem(id) {
        var that = this
        hllayer.confirm1('此操作不可逆转，确定删除联系人吗？', function() {
          hlhttp.delete({
            url: 'market/expansion_proj_contacts?id=' + id,
            data: {
              id: this.id,
            },
            success: (data) => {
              that.getContact()
            }
          })
        })
      },
      //***  相关文件
      previewImage: function(url) {
        var imageExt = ['jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG'];
        var pdfExt = ['pdf', 'PDF'];
        var officeExt = ['docx','doc','xlsx','xls','xlt']
        var imageFlag = false;
        var pdfFlag = false;
        var officeFlag = false;

        imageExt.forEach((e, i) => {
          if (url.indexOf(e) !== -1) imageFlag = true;
        })

        if (imageFlag) {
          $.imageShow([url])
          return
        }
        pdfExt.forEach((e, i) => {
          if (url.indexOf(e) !== -1) pdfFlag = true;
        })
        if (pdfFlag) {
          $.previewPDF(url)
          return
        }

        officeExt.forEach((e, i) => {
          if (url.indexOf(e) !== -1) officeFlag = true
        })
        if (officeFlag) {
          $.previewOffice(url)
          return
        }
        hl.toast('暂不支持该格式')
      },
      getfileData() {
        hlhttp.get({
          url: 'market/expansion_proj_file',
          data: {
            ref_id: this.id,
          },
          success: (data) => {
            this.attachmentsList = data.projFileList
          }
        })
      },
      delfileItem(id) {
        var that = this
        hlhttp.delete({
          url: 'market/expansion_proj_file?id=' + id,
          data: {
            id: id,
          },
          success: (data) => {
            that.getfileData()
          }
        })
      },
      uploadFile: function() {
        if (this.progress) {
          hl.toast('文件尚未上传完成')
          return
        }
        $('.upload-file-button').trigger('click')
      },
      //转运营
      runOperate() {
        let data = {
          projName: this.data.expansionProjInfo.projName,
          id: this.data.expansionProjInfo.id,
          projLegalName: this.data.expansionProjInfo.projLegalName,
          companyName: this.data.expansionProjInfo.companyName,
          companyId: this.data.expansionProjInfo.companyId,
          companyName: this.data.expansionProjInfo.companyName,
          "provinceId": this.data.expansionProjInfo.provinceId, // 省id
          "provinceName": this.data.expansionProjInfo.provinceName, // 省名
          "cityId": this.data.expansionProjInfo.cityId, // 城市id
          "cityName": this.data.expansionProjInfo.cityName, // 城市名
          "districtId": this.data.expansionProjInfo.districtId, // 区id
          "districtName": this.data.expansionProjInfo.districtName, // 区名
          "address": this.data.expansionProjInfo.address,
          "areaCoveredAcreage": this.data.expansionProjInfo.areaCoveredAcreage,
          "buildingCoveredAcreage": this.data.expansionProjInfo.buildingCoveredAcreage,
          "rentAcreage": this.data.expansionProjInfo.rentAcreage,
          "projPrOwnerName": this.data.expansionProjInfo.projPrOwnerName,
          "contactName": this.data.expansionProjInfo.contactName,
          "contactPhoneNum": this.data.expansionProjInfo.contactPhoneNum,
          "rightsInterestsRates": this.data.expansionProjInfo.rightsInterestsRates,
          "landUseRightsExpireDate": this.data.expansionProjInfo.landUseRightsExpireDate,
          "propertyAccessionDate": this.data.expansionProjInfo.propertyAccessionDate,
          "propertyProfile": this.data.expansionProjInfo.propertyProfile,
          "companyFullName": this.data.expansionProjInfo.companyFullName,
          "legalPersonName": this.data.expansionProjInfo.legalPersonName
        }
        window.localStorage.removeItem('BUILDINGID');
        window.sessionStorage.removeItem('NAVPOSTION');
        window.localStorage.removeItem('templateID');
        window.localStorage.removeItem('expenditureIDS');
        hlUrlUtil.goTo('projectConfigIndex', {
          data: escape(JSON.stringify(data)),
          ddtab: true,
          market: true
        }, true)
      }
    },
    mounted() {
      if (hlUrlUtil.GetQueryString('first') == 'JL') {
        this.currentTab = '跟进记录'
      } else {
        this.currentTab = '项目详情'
      }
      this.getData();
      this.getReasonList();
      this.getfollowData();
      this.getContact();
      this.getfileData();
      hlSelectUi()
      var that = this;
      fileUpload('.upload-file-button', uploadUrl, function(res) {
        var projectId = hlUrlUtil.GetQueryString('projectId');
        that.progress = ''
        hlhttp.post({
          url: 'market/expansion_proj_file',
          data: {
            ref_id: that.id,
            name: res.json.name,
            url: res.json.url
          },
          success: function() {
            hl.toast('上传成功')
            // that.contractFile.push(res.json)
            that.getfileData();
          }
        })
      }, function(val) {
        that.progress = parseInt((val.loaded / val.total) * 100) + '%'
      });
    },
  })
}())
